#define MSFIFO_C
#include <sys/types.h>
#include <sys/stat.h>		//mkdir()
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <libmscommon/mscommon.h>
#include <libmscommon/msfifo.h>
#include <libmslog/mslog.h>

#define FLAG "FIFO_SAMPLE"

void msfifoblock_test(void)
{
	/*create a fifo context*/
	MSFIFOBLOCKContext *pmsfifo_ctt = msfifo_api_blockInit(1024,"test");
	char buf[128]={0};
	char index=0;
	char len=64;
	/* fill data */
	for (index= 0; index<len; index++){
		buf[index]=index;
    	}
	ms_bufHex("msfifoblock:BF-w", buf, len);
	msfifo_api_blockIn(pmsfifo_ctt, buf, len, NULL);
	ms_memset(buf, 0, sizeof(buf));
	/* read data */
	msfifo_api_blockOut(pmsfifo_ctt, buf, len, NULL);
	ms_bufHex("msfifoblock:AF-r", buf, len);
	ms_bufHex("msfifoblock:BF-wl", buf, len);
	msfifo_api_blockLenIn(pmsfifo_ctt, buf, len);
	ms_memset(buf, 0, sizeof(buf));
	len=msfifo_blockLenOut(pmsfifo_ctt, buf, sizeof(buf));
	if(len>0){
		ms_bufHex("msfifoblock:AF-rl", buf, len);
	}else{
		ms_waring("msfifo_blockLenOut error");
	}
	msfifo_api_blockDeinit(&pmsfifo_ctt);
}

void msfifolist_test(void)
{
	char index=0;
	char *pbuf=ms_null;
	MSFIFOLISTContext * pheader_fifolist_ctt=ms_null;
/*push list1 to fifo*/	
	MSFIFOLISTContext *pmsfifolist_ctt1=msfifo_api_listMalloc(16,,"pmsfifolist_ctt1");
	pbuf=pmsfifolist_ctt1->ptr;
	for (index= 0; index<pmsfifolist_ctt1->len; index++){
		pbuf[index]=index;
    	}
	ms_bufHex("msfifolist1:BF-rl", pbuf, pmsfifolist_ctt1->len);
	msfifo_api_listIn(&pheader_fifolist_ctt, pmsfifolist_ctt1);
/*push list2 to fifo*/	
	MSFIFOLISTContext *pmsfifolist_ctt2=msfifo_api_listMalloc(32,"pmsfifolist_ctt2");
	pbuf=pmsfifolist_ctt2->ptr;
	for (index= 0; index<pmsfifolist_ctt2->len; index++){
		pbuf[index]=index;
    	}
	ms_bufHex("msfifolist2:BF-rl", pbuf, pmsfifolist_ctt2->len);
	
	msfifo_api_listIn(&pheader_fifolist_ctt, pmsfifolist_ctt2);


/*pull list1 from fifo*/	
	ms_debug("total num:%d",msfifo_api_listTotalNum(&pheader_fifolist_ctt));
	MSFIFOLISTContext *pmsfifolist_ctt_out1=msfifo_api_listOut(&pheader_fifolist_ctt);
	ms_debug("total num:%d",msfifo_api_listTotalNum(&pheader_fifolist_ctt));
	if(ms_null!=pmsfifolist_ctt_out1){
		pbuf=pmsfifolist_ctt_out1->ptr;
		ms_bufHex("msfifolist1:AF-rl", pbuf, pmsfifolist_ctt_out1->len);
		msfifo_api_listDemalloc(&pmsfifolist_ctt_out1);
	}
/*pull list2 from fifo*/
	ms_debug("total num:%d",msfifo_api_listTotalNum(&pheader_fifolist_ctt));
	MSFIFOLISTContext *pmsfifolist_ctt_out2=msfifo_api_listOut(&pheader_fifolist_ctt);
	ms_debug("total num:%d",msfifo_api_listTotalNum(&pheader_fifolist_ctt));
	if(ms_null!=pmsfifolist_ctt_out2){
		pbuf=pmsfifolist_ctt_out2->ptr;
		ms_bufHex("msfifolist2:AF-rl", pbuf, pmsfifolist_ctt_out2->len);
		msfifo_api_listDemalloc(&pmsfifolist_ctt_out2);
	}
}
int main(void)
{
	mslog_api_init( 53, ms_null,ms_null, ms_null);
	//msfifoblock_test();
	msfifolist_test();
	mslog_api_deinit();
	return 0;
}
#undef MSFIFO_C
